/**
 * 
 */
package com.acminds.fotoporter.ui.controllers;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.event.AjaxBehaviorEvent;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

import com.acminds.fotoporter.persistence.dto.Image;
import com.acminds.fotoporter.persistence.dto.OrderRequest;
import com.acminds.fotoporter.service.customer.OrderRequestService;
import com.acminds.fotoporter.ui.BaseAction;
import com.acminds.fotoporter.ui.ImageHolder;
import com.acminds.fotoporter.utils.ImageUtils;

/**
 * @author MANSUR
 * 
 */
@ManagedBean
@SessionScoped
public class OrderAction extends BaseAction implements ImageHolder {

	private ArrayList<Image> photos = new ArrayList<Image>();
	@ManagedProperty(value="#{orderRequest}")
	private OrderRequest orderRequest;
	private String orderNumber;
	private int currentPhotoIndex;
	private OrderRequestService service = new OrderRequestService();
	
	@Override
	public List<Image> getImages() {
		return photos;
	}
	public void setImages(ArrayList<Image> photos) {
		this.photos = photos;
	}
	public String getOrderNumber() {
		return orderNumber;
	}
	public void setOrderNumber(String orderNumber) {
		this.orderNumber = orderNumber;
	}
	public OrderRequestService getService() {
		return service;
	}
	public void setService(OrderRequestService service) {
		this.service = service;
	}
	public int getSize() {
		return photos.size();
	}
	public OrderRequest getOrderRequest() {
		return orderRequest;
	}
	public void setOrderRequest(OrderRequest orderRequest) {
		this.orderRequest = orderRequest;
	}
	public int getCurrentPhotoIndex() {
		return currentPhotoIndex;
	}
	public void setCurrentPhotoIndex(int currentPhotoIndex) {
		this.currentPhotoIndex = currentPhotoIndex;
	}
	
	public String deletePhoto() {
		getImages().remove(getCurrentPhotoIndex());	
		orderRequest.getItemOrdereds().remove(getCurrentPhotoIndex());
		return null;
	}

	public String clearUploadData() {
		photos.clear();
		orderRequest.getItemOrdereds().clear();
		return null;
	}
	
	public String updateOrder() {
		return null;
	}
	
	public String submitOrder() {
		return null;
	}
	
	public String orderStatus() {
		return null;
	}
	
	public void fileUploadListener(FileUploadEvent e) {
		UploadedFile item = e.getUploadedFile();
        Image file = new Image();
        file.setImageName(item.getName());
        file.setImageData(item.getData());
        file.setPreviewData(ImageUtils.reScaleMultiStep(item.getData(), 600));        
        file.setThumbnailData(ImageUtils.reScaleMultiStep(item.getData(), 200));
        file.setMimeType(item.getContentType());
        photos.add(file);        
	}
	
	public void prepareOrder(AjaxBehaviorEvent e) {
		service.prepareOrder(orderRequest, photos);		
	}
	
	public void paint(OutputStream stream, Object object) throws IOException {
		stream.write(orderRequest.getItemOrdereds().get((Integer) object).getImage().getPreviewData());        
		stream.close();
    }
	
	public void paintThumbnail(OutputStream stream, Object object) throws IOException {
		stream.write(((Image)orderRequest.getItemOrdereds().get((Integer) object).getImage()).getThumbnailData());        
		stream.close();
    }
}
